import { request } from '../../request/index.js'

Page({
  data: {
    ids: -1,// 校区 id
    phone: '',
    wxnum: '',
    qqnum: '',
    stuId: '',
    campus: ['南湖校区', '鉴湖校区', '马房山校区', '余家头校区'],
    address: '',
    classid: ''
  },
  onLoad() {
    this.init();
  },
  async init() {
    // 初始化信息
    await this.getDetail();
    this.handleCampus();
  },
  // 标签选择
  choose(e) {
    let that = this;
    that.setData({
      ids: e.detail.value
    })
    switch (this.data.ids) {
      case "0": this.setData({ address: "南湖校区" }); break;
      case "1": this.setData({ address: "鉴湖校区" }); break;
      case "2": this.setData({ address: "马房山校区" }); break;
      case "3": this.setData({ address: "余家头校区" }); break;
    }
  },
  // 初始化信息
  async getDetail() {
    const result = await request({
      url: '/secondhand/getUserDetail',
    })
    console.log("result ", result.data.obj)
    const {address,phoneNumber:phone,wxNumber:wxnum,qqNumber:qqnum,grade:classid}=result.data.obj;
    this.setData({
      // address,
      // 后端数据不规范，先写死了，规范之后再改回去
      address: '鉴湖校区',
      phone,
      wxnum,
      qqnum,
      classid
    })
  },
  // 处理校区信息
  handleCampus() {
    console.log(this.data.address)
    switch (this.data.address) {
      case "南湖校区": this.setData({ ids: 0 }); break;
      case "鉴湖校区": this.setData({ ids: 1 }); break;
      case "马房山校区": this.setData({ ids: 2 }); break;
      case "余家头校区": this.setData({ ids: 3 }); break;
    }
  },
  // 从输入框中获取用户输入
  wxInput(e) {
    this.setData({
      wxnum: e.detail.value
    })
  },
  qqInput(e) {
    this.setData({
      qqnum: e.detail.value
    })
  },
  phInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  classInput(e) {
    this.setData({
      classid: e.detail.value
    })
  },
  // 上传用户信息
  async update() {
    const{classid,qqnum,phone,address,wxnum}=this.data;
    // 整理信息
    let userInfo = {
      grade: classid,
      qqNumber: qqnum,
      phoneNumber: phone,
      address,
      wxNumber: wxnum
    }
    // 上传
    const result = await request({
      url: '/secondhand/updateUser',
      method: 'POST',
      data: userInfo
    })
    console.log(result)
    // 如果成功，更新用户信息状态
    if (result.data.obj === 'SUCCESS') {
      wx.setStorageSync('loginStatus', 2) // 登录状态 未定义-未登录 1-已登录 2-信息已完善
    }
    // 返回上一页
    wx.navigateBack({
      delta: 1,
    })
  }
})